package vip.xiaonuo.core.util;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 随机字符串
 * @author yaohuaipeng
 * @date 2018/10/26-16:16
 */
public class StrUtils {
    /**
     * 把逗号分隔的字符串转换字符串数组
     *
     * @param str
     * @return
     */
    public static String[] splitStr2StrArr(String str,String split) {
        if (str != null && !str.equals("")) {
            return str.split(split);
        }
        return null;
    }

    public static String generateCode() {
        String charList = "ABCDEFGHIJKLMNPQRSTUVWXY";
        String numList = "0123456789";
        String rev = "";
        int maxNumCount = 4;
        int length = 6;
        Random f = new Random();
        for (int i = 0; i < length; i++) {
            if (f.nextBoolean() && maxNumCount > 0) {
                maxNumCount--;
                rev += numList.charAt(f.nextInt(numList.length()));
            } else {
                rev += charList.charAt(f.nextInt(charList.length()));
            }
        }
        return rev;
    }


    public static String getRandomString(int length) {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(10);
            sb.append(str.charAt(number));
        }
        return sb.toString();

    }
    public static String createRandom( int length){
        String retStr = "";
        String strTable ="1234567890";
        String str ="1234567890";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                if (i == 0){
                    double dblR = Math.random() * str.length();
                    int intR = (int) Math.floor(dblR);
                    char c = str.charAt(intR);
                    if (('1' <= c) && (c <= '9')) {
                        count++;
                    }
                    retStr += strTable.charAt(intR);
                }else {
                    double dblR = Math.random() * len;
                    int intR = (int) Math.floor(dblR);
                    char c = strTable.charAt(intR);
                    if (('0' <= c) && (c <= '9')) {
                        count++;
                    }
                    retStr += strTable.charAt(intR);
                }
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);
        return retStr;
    }



    public static String getOrderIdByTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        return newDate+result;
    }

}
